<template>
    <div>
        <x-button @click.native="pay">付款</x-button>
        <x-button @click.native="testpay">模拟付款</x-button>
        <!--<x-button @click.native="go">回头</x-button>-->
    </div>
</template>
<script>
    import { XButton } from 'vux'
    import user from '../../api/user.js'
    export default {
        components:{
            XButton
        },
        methods:{
            go(){
                this.$router.push({path:'/lottery'})
            },
            data(){
                return {
                    user
                }
            },
            pay(){
                this._getWxpayData()
            },
            _getWxpayData() {
                var _ = this
                this.$axios.get('/lottery/wxpay?openid=' + user.userinfo.openid).then((res)=>{
                // 这里的openid我存在了localStorage里面，获取授权进入时就进行了一次存入，方便调用。
                    console.log(res)
                    res = res.data
                    console.log(res)
                    if (res.status === 1) {
                        console.log('434:yes')
                        _._wxpayConfig()
                        wx.ready(function(){
                            _._setWxpayInfo(res.data)
                        })
                    }
                })
            },
            _wxpayConfig() {
                this.$axios.get('/lottery/signa').then(function(res){
                    console.log(res.body)
                    // let data = res.body.data
                    var data = res.data
                    // if (res.body.status === 1) {
                    if (data.status === 1) {
                        alert('config')
                        wx.config({
                            // debug: true,
                            appId: data.appId,
                            timestamp: data.timestamp,
                            nonceStr: data.nonceStr,
                            signature: data.signature,
                            jsApiList: ['chooseWXPay']
                        })
                    }
                })
            },
            _setWxpayInfo(data) {
                console.log('pay')
                var _ = this
                // _.$router.go('/setting')
                wx.chooseWXPay({
                    appId: data.appId,
                    timestamp: data.timeStamp,
                    nonceStr: data.nonceStr,
                    package: data.package,
                    signType: data.signType,
                    paySign: data.paySign,
                    success(res) {
                        // alert(res.errMsg)
                        if (res.errMsg === 'chooseWXPay:ok') {
                            // alert(user.status)
                            // window.alert('支付成功')
                            window.alert('支付成功')
                            // user.status = 1
                            // alert(user.status)
                            // _.$axios.post('/lottery/putMoney',{
                            //     trade_no:res.trade_no,
                            //     openid:user.userinfo.openid
                            // }).then(()=>{
                            _.$router.push({path:'/lottery?trade_no='+data.trade_no})
                            // })
                            
                            // _.$router.go(0)
                            // window.location.reload()
                            // _.$router.go('/setting')
                        } else {
                            window.alert(' 支付失败')
                            // window.location.reload()
                        }
                    },
                    cancel() {
                        window.alert('支付取消')
                        // window.location.reload()
                        _.$router.push({path:'/lottery'})
                        // user.status = 1
                    },
                    error(res) {
                        window.alert('支付失败')
                        // window.location.reload()
                        _.$router.push({path:'/lottery'})
                    }
                })
            },
            testpay(){
                window.alert('支付成功')
                this.$router.push({path:'/lottery?trade_no=11111111'})
            }
        },
        mounted(){
            // alert('进入pay')
        }
    }
</script>